<template>
    <div class="login main">
        <h1 class="title">欢迎登陆<span style="color: #42bd56;">笔记之家</span></h1>
        <form method="post" @submit.prevent="onSubmit()">
            <div class="form_tel">
                <p class="text">手机号码</p>
                <label>
                    <input type="text" ref="tel" placeholder="手机号码" name="tel" v-model.trim="tel" maxlength="11" />
                </label>
            </div>
            <div class="form_pwd">
                <p class="text">密码</p>
                <label>
                    <!-- <template v-if="passType === 'password'">
                        <input v-model.trim="pwd" type="password" ref="pwd" name="pwd" placeholder="密码">
                    </template>
                    <template v-if="passType === 'text'">
                        <input v-model.trim="pwd" type="text" ref="pwd" name="pwd" placeholder="密码">
                    </template> -->
                    <input v-model.trim="pwd"  :type="passType" ref="pwd" name="pwd" placeholder="密码">

                </label>
                <span class="show_pwd" :class="{show: passType == 'text'}" @click="showPwd()"></span>
            </div>
            <!-- <div style="margin-bottom: 0px;">
                <p class="text">{{msgTip}}</p>
            </div> -->
            <router-link :to="{name:'register',query:{query:$route.query.query || ''}}" style="color:#42bd56;font-weight:700">注册</router-link>
            <div class="form_submit">
                <button class="submit" type="submit" :disabled="isDisabled"
                    :class="{disabled: isDisabled}">{{loginState}}</button>
            </div>
        </form>
    </div>
</template>
<script>
    export default {
        name: "register",
        data() {
            return {
                isDisabled: false,
                loginState: "登陆",
                passType: 'password',
                tel: '',
                pwd: '',
                token: '',
                msgTip: ""
            }
        },
        methods: {
            showPwd: function () {
                this.passType =   this.passType == 'text' ?  'password' : "text";
            },
            beforeSubmit: function () {
                this.isDisabled = true
                this.loginState = '正在登陆...'
            },
            onSuccess: function (res) {
                //console.log(res)
                //localStorage.setItem('token', res.token);
               // localStorage.setItem('userTel', res.tel);
                // localStorage.setItem('userId', res._id);
                this.$utils.setUserId(res._id);
                
                this.$store.commit("setUserInfo",res);
                //var backUrl = localStorage.getItem("backUrl");
                var backUrl = this.$route.query.query || "";
                if (backUrl) { //如果 backUrl  存在  说明是通过其他页面跳过来的  需要跳回去
                    window.location.href = backUrl;
                } else {
                    this.$router.push("/");
                }                
            },
            onError: function (err) {
                this.loginState = '登陆'
                this.isDisabled = false
            },
            onSubmit: function () {
                this.beforeSubmit()
                // let pwd = this.$refs.pwd.value;
				let pwd = this.pwd;
				let tel = this.tel;
                this.$api.login({
                    tel: tel,
                    password: pwd,
                }).then(res => {
                    //console.log(res);
                    this.onSuccess(res)
                }, err => {
                    this.onError(err)
                })
            }
        }
    }
</script>
<style lang="scss" scoped>
    .require {
        color: red;
    }

    .main {
        width: 600px;
        margin: 0 auto;

        .title {
            width: 100%;
            height: 80px;
            line-height: 80px;
            text-align: center;
        }

        form {
            padding: 15px;

            div {
                margin-bottom: 15px;

                .text {
                    height: 20px;
                    font-size: 14px;
                    color: #aaa;
                    margin-bottom: 5px;

                    .tip {
                        /* color: #ccc; */
                        color: red;
                    }

                }

                label {
                    display: block;
                    width: 100%;
                    height: 60px;

                    input::placeholder {
                        color: #ccc;
                    }

                    input {
                        width: 100%;
                        height: 100%;
                        padding: 0 10px;
                        box-sizing: border-box;
                        font-size: 1.5rem;
                        background: #fff;
                        border: 1px solid #ccc;
                        border-radius: 10px;
                        outline: 0;
                    }
                }
            }

            .form_pwd {
                position: relative;

                input {
                    padding-right: 80px;
                }

                .show_pwd {
                    position: absolute;
                    right: 0;
                    top: 25px;
                    width: 80px;
                    height: 60px;
                    background: url("../assets/pwd_hide.png") center center no-repeat;
                    background-size: 36px;
                }

                .show_pwd.show {
                    background: url("../assets/pwd_show.png") center center no-repeat;
                    background-size: 36px;
                }
            }

            .form_submit {
                .submit {
                    cursor: pointer;
                    width: 100%;
                    height: 4.4rem;
                    margin-top: 1rem;
                    border: 0.1rem solid #17AA52;
                    border-radius: 0.3rem;
                    width: 100%;
                    height: 100%;
                    font-size: 1.7rem;
                    line-height: 4.4rem;
                    text-align: center;
                    color: #fff;
                    background: #42bd56;
                }

                .disabled {
                    cursor: not-allowed;
                    background: #eee;
                    border: none;
                }
            }
        }
    }
</style>